package com.hupu.jrs.widget.photodraweeview

import android.content.Context
import android.graphics.Canvas
import android.util.AttributeSet
import android.view.GestureDetector
import android.view.MotionEvent
import com.facebook.drawee.generic.GenericDraweeHierarchy
import com.facebook.drawee.view.SimpleDraweeView

class PhotoDraweeView : SimpleDraweeView, IAttacher {

    private var mAttacher: Attacher? = null

    override var minimumScale: Float
        get() = mAttacher!!.minimumScale
        set(minimumScale) {
            mAttacher!!.minimumScale = minimumScale
        }

    override var mediumScale: Float
        get() = mAttacher!!.mediumScale
        set(mediumScale) {
            mAttacher!!.mediumScale = mediumScale
        }

    override var maximumScale: Float
        get() = mAttacher!!.maximumScale
        set(maximumScale) {
            mAttacher!!.maximumScale = maximumScale
        }

    override var scale: Float
        get() = mAttacher!!.scale
        set(scale) {
            mAttacher!!.scale = scale
        }

    override var onPhotoTapListener: OnPhotoTapListener
        get() = mAttacher!!.onPhotoTapListener!!
        set(listener) {
            mAttacher!!.onPhotoTapListener = listener
        }

    override var onViewTapListener: OnViewTapListener
        get() = mAttacher!!.onViewTapListener!!
        set(listener) {
            mAttacher!!.onViewTapListener = listener
        }

    constructor(context: Context, hierarchy: GenericDraweeHierarchy) : super(context, hierarchy) {
        init()
    }

    constructor(context: Context) : super(context) {
        init()
    }

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
        init()
    }

    constructor(context: Context, attrs: AttributeSet, defStyle: Int) : super(context, attrs, defStyle) {
        init()
    }

    protected fun init() {
        if (mAttacher == null || mAttacher!!.draweeView == null) {
            mAttacher = Attacher(this)
        }
    }

    override fun onTouchEvent(event: MotionEvent): Boolean {

        return super.onTouchEvent(event)
    }

    override fun onDraw(canvas: Canvas) {
        val saveCount = canvas.save()
        canvas.concat(mAttacher!!.drawMatrix)
        super.onDraw(canvas)
        canvas.restoreToCount(saveCount)
    }

    override fun onAttachedToWindow() {
        init()
        super.onAttachedToWindow()
    }

    override fun onDetachedFromWindow() {
        mAttacher!!.onDetachedFromWindow()
        super.onDetachedFromWindow()
    }

    override fun setScale(scale: Float, animate: Boolean) {
        mAttacher!!.setScale(scale, animate)
    }

    override fun setScale(scale: Float, focalX: Float, focalY: Float, animate: Boolean) {
        mAttacher!!.setScale(scale, focalX, focalY, animate)
    }

    override fun setZoomTransitionDuration(duration: Long) {
        mAttacher!!.setZoomTransitionDuration(duration)
    }

    override fun setAllowParentInterceptOnEdge(allow: Boolean) {
        mAttacher!!.setAllowParentInterceptOnEdge(allow)
    }

    override fun setOnDoubleTapListener(listener: GestureDetector.OnDoubleTapListener) {
        mAttacher!!.setOnDoubleTapListener(listener)
    }

    override fun setOnScaleChangeListener(listener: OnScaleChangeListener) {
        mAttacher!!.setOnScaleChangeListener(listener)
    }

    override fun setOnLongClickListener(listener: OnLongClickListener) {
        mAttacher!!.setOnLongClickListener(listener)
    }

    override fun update(imageInfoWidth: Int, imageInfoHeight: Int) {
        mAttacher!!.update(imageInfoWidth, imageInfoHeight)
    }
}
